import { pick } from 'ramda'
import { ref, h } from 'vue'
import Pagination from './index.vue'
export enum PageType {
  new
}
export interface getPaginationDto {
  pageNum: number
  pageSize: number
}
export interface Pagination {
  showTotal?: (total: number) => string | boolean
  pageSizeOptions?: Array<string>
  showQuickJumper?: boolean
  showSizeChanger?: boolean
}

export interface DefaultConfig extends Pagination {
  pageSize: number
}
/**
 * 集成分页
 * @param search 搜索函数
 * @param defaultConfig  分页参数
 * @returns {Object} 包含分页组件参数、组件
 * @returns {number} Object.total - 总数
 * @returns {number} Object.pageSize - 数量
 * @returns {number} Object.current - 页数
 * @returns {Function} Object.renderPagination - 分页组件，按组件方式使用
 * @returns {Function} Object.getPagination - 获取当前页数
 */
export function usePagination(search: (type?: PageType) => void, defaultConfig?: DefaultConfig) {
  const total = ref(0)
  const pageSize = ref(defaultConfig?.pageSize ?? 10)
  const current = ref(0)
  function getPagination(is = false): getPaginationDto {
    if (is) {
      current.value = 1
    }
    return {
      pageNum: current.value,
      pageSize: pageSize.value
    }
  }
  function renderPagination(props?: Pagination) {
    const prop = pick(
      ['showTotal', 'pageSizeOptions', 'showQuickJumper', 'showSizeChanger'],
      props ?? defaultConfig ?? {}
    )
    return h(Pagination, {
      search, defaultConfig, total, current, pageSize, ...prop,
    })
  }
  return {
    total,
    pageSize,
    current,
    renderPagination,
    getPagination
  }
}
